<script setup>
import { reactive, ref } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import { useRouter } from 'vue-router'
import { login } from '@/api/common/user.js'
import { showMessage } from "@/composables/util.js";
import { setToken } from "@/composables/cookie.js";

const router = useRouter()
// 表单引用
const formRef = ref(null)
// 表单验证规则
const rules = {
  username: [
    {
      required: true,
      message: '用户名不能为空',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'blur',
    }
  ],
  role: [
    {
      required: true,
      message: '必须选择一个角色',
      trigger: 'change',
    }
  ]
}

// 定义响应式的表单对象
const form = reactive({
  username: '',
  password: '',
  role: ''
})
// 点击登录触发onSubmit函数
const onSubmit = () => {
    formRef.value.validate((valid) => {
      if (!valid) {
        console.log('表单验证不通过')
        return false
      }
      // 向后端调用登录接口
      login(form).then((res) => {
        // 判断登录是否成功
        if (res.data.success === true) {
          // 提示登录成功
          showMessage('登录成功')
          // 存储用户信息JSON格式到Local Storage(浏览器本地存储) 中
          localStorage.setItem("userInfo", JSON.stringify(res.data.data))
          // 存储 Token 到 Cookie 中
          setToken(res.data.data.token)

          // 根据角色跳转到对应页面
          if(form.role === 'ADMIN'){
            router.push('/manage/admin/home')
          }else if(form.role === 'BLOGGER'){
            router.push('/manage/blogger/home')
          }else {
            router.push('/')
          }

        } else {
          let message = res.data.message
          console.log(message)
          // 提示消息
          showMessage(message, 'error')
        }
      })
    })
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="illustration">
        <img
            src="../../assets/login.png"
            alt="登录插画"
            class="floating-image"
        >
        <div class="decorative-circle"></div>
      </div>

      <div class="form-wrapper">
        <el-form
            ref="formRef"
            :rules="rules"
            :model="form"
            class="animated-form"
        >
          <h1 class="login-title">
            <span class="welcome-text">欢迎登录</span>
            <div class="animated-dot">~</div>
          </h1>

          <!-- 用户名 -->
          <el-form-item prop="username">
            <div class="input-group">
              <el-input
                  v-model="form.username"
                  :prefix-icon="User"
                  size="large"
                  placeholder="请输入用户名"
              />
            </div>
          </el-form-item>

          <!-- 密码 -->
          <el-form-item prop="password">
            <div class="input-group">
              <el-input
                  v-model="form.password"
                  :prefix-icon="Lock"
                  size="large"
                  show-password
                  placeholder="请输入密码"
              />
            </div>
          </el-form-item>

          <el-form-item prop="role">
            <el-radio-group v-model="form.role" class="original-role-group">
              <el-radio value="ADMIN" size="large">管理员</el-radio>
              <el-radio value="BLOGGER" size="large">博主</el-radio>
              <el-radio value="USER" size="large">用户</el-radio>
            </el-radio-group>
          </el-form-item>

          <!-- 登录按钮 -->
          <el-form-item>
            <el-button
                type="primary"
                class="login-btn"
                @click="onSubmit"
            >
              立即登录
              <span class="arrow">→</span>
            </el-button>
          </el-form-item>

          <div class="register-link">
            还没有账号?
            <span @click="router.push('/register')">立即注册</span>
          </div>
          <!-- <div class="register-link">
            忘记密码?
            <span @click="router.push('/register')">点此找回</span>
          </div> -->
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  /* 方案三 - 沉浸式 */
  background: linear-gradient(
          145deg,
          rgba(64,158,255,0.8) -20%,
          rgba(64, 158, 255, 0.4) 40%,
          rgba(255,255,255,0.8) 120%
  );
  /* 方案四 - 现代感双色 */
  //background: linear-gradient(
  //        to right bottom,
  //        #409EFF 0%,
  //        #7ab8ff 80%,
  //        #d2e7ff 100%
  //);
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    width: 300vw;
    height: 300vw;
    background: radial-gradient(circle, rgba(64,158,255,0.05) 0%, transparent 60%);
    animation: gradient-rotate 20s linear infinite;
  }
}

.login-box {
  display: flex;
  width: 60%;
  max-width: 1000px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 40px rgba(64,158,255,0.15);
  overflow: hidden;
  transform: translateY(0);
  animation: box-float 4s ease-in-out infinite;
}

.illustration {
  flex: 1;
  position: relative;
  overflow: hidden;

  .floating-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: transform 0.3s ease-out;
  }

  .decorative-circle {
    position: absolute;
    width: 200px;
    height: 200px;
    background: rgba(64,158,255,0.1);
    border-radius: 50%;
    filter: blur(60px);
    bottom: -50px;
    right: -50px;
  }
}

.form-wrapper {
  flex: 1;
  padding: 40px;
  position: relative;
}

.original-role-group {
  width: 70%;
  display: flex;
  justify-content: space-between;
  gap: 15px;

  :deep(.el-radio) {
    margin: 0;
    height: 40px;
    line-height: 40px;
    .el-radio__label {
      font-size: 14px;
      padding-left: 8px;
      transition: color 0.3s;
    }

    .el-radio__inner {
      width: 18px;
      height: 18px;
      border-width: 1px;

      &::after {
        width: 8px;
        height: 8px;
      }
    }

    &.is-checked {
      .el-radio__inner {
        background-color: #409EFF;
        border-color: #409EFF;
      }
      .el-radio__label {
        color: #409EFF;
        font-weight: 500;
      }
    }
  }
}

.login-title {
  text-align: center;
  margin-bottom: 40px;
  position: relative;
  color: #409EFF;
  .welcome-text {
    display: inline-block;
    font-size: 24px;
    font-weight: 600;
    color: #409EFF;
    transform: translateY(0);
    animation: text-float 3s ease-in-out infinite;
  }

  .animated-dot {
    display: inline-block;
    animation: dot-bounce 1.2s infinite;
  }
}

.input-group {
  position: relative;
  margin-bottom: 24px;

  :deep(.el-input__wrapper) {
    background: #f8fbff;
    border-radius: 8px;
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 0 0 2px rgba(64,158,255,0.3);
    }

    &.is-focus {
      box-shadow: 0 0 0 2px rgba(64,158,255,0.5);
    }
  }

}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  transition: all 0.3s ease !important;

  .arrow {
    opacity: 0;
    transform: translateX(-10px);
    transition: all 0.3s ease;
  }

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(64,158,255,0.3);

    .arrow {
      opacity: 1;
      transform: translateX(0);
    }
  }
}

/* 统一输入元素宽度 */
.input-group,
.login-btn {
  width: 100%;
}

.register-link {
  text-align: center;
  color: #606266;
  margin-top: 24px;

  span {
    color: #409EFF;
    cursor: pointer;
    position: relative;

    &::after {
      content: '';
      position: absolute;
      bottom: -2px;
      left: 0;
      width: 0;
      height: 1px;
      background: #409EFF;
      transition: width 0.3s ease;
    }

    &:hover::after {
      width: 100%;
    }
  }
}

/* 调整表单验证提示位置 */
:deep(.el-form-item) {
  .el-form-item__error {
    top: -18px; // 上移位置
    &::before {
      content: "⚠ ";
    }
  }
}

/* 动画定义 */
@keyframes box-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

@keyframes gradient-rotate {
  from { transform: rotate(0); }
  to { transform: rotate(360deg); }
}

@keyframes text-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

@keyframes dot-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}
</style>